// Get the path of the currently running do-file
local do_file_path `c(pwd)'

// Change the working directory to the location of the do-file
cd "`do_file_path'"

clear
use "proc_data\TREAT.dta" 
keep objectid bhbrd
keep if bhbrd==1
save "proc_data\TREATbhbr.dta", replace

**
** 1)Total area of group
** 2)Area of group in Nigeria
** 3)Area group within Nigeria-BH

import dbase using "proc_data\greg_intersect_join_pt.dbf", clear

rename *, lower

keep objectid gid_0 fid_greg greg_km2 group1-g3longnam

* dummy coastline misalignment, used spatial join closest to overcome
rename fid_greg d_gregcoast
replace d_gregcoast=1 if d_gregcoast>=0
replace d_gregcoast=0 if d_gregcoast==-1
*sum d_gregcoast, det

merge m:1 objectid using "proc_data\TREATbhbr.dta" 

sort objectid
by objectid: egen grid_km2 = total(greg_km2)

* total in Nigeria

** GROUP LEVEL 1

* total area of ethnic group (anywhere)
sort g1id
by g1id: egen ethg1_km2 = total(greg_km2)

* total area of ethnic group in Nigeria
sort g1id
g ethg1_nga_km2_only = greg_km2 if gid_0=="NGA"
sort gid_0 
*br if ethg1_nga_km2_only!=. & gid_0!="NGA"
sort g1id
by g1id: egen ethg1_nga_km2 = total(ethg1_nga_km2_only)

* total area of ethnic group in cell's country
sort g1id gid_0
by g1id gid_0: egen ethg1_ctr_km2 = total(greg_km2)

sort g1id
g ethg1_bhbr_km2_only = greg_km2 if bhbrd==1
sort g1id
by g1id: egen ethg1_bhbr_km2 = total(ethg1_bhbr_km2_only) 


drop ethg1_nga_km2_only
drop ethg1_bhbr_km2_only

drop _merge

label variable d_gregcoast "Dummy coast"

label variable ethg1_km2 "Total area of ethnic group (level 1) : GREG"
label variable ethg1_nga_km2 "Area of ethnic group (level 1) in NGA : GREG"
label variable ethg1_bhbr_km2 "Area of ethnic group (level 1) in BHbr : GREG"
label variable ethg1_ctr_km2 "Area of ethnic group (level 1) in cell's country : GREG'"
label variable greg_km2 "Area of ethnic group shape x grid : GREG"
label variable grid_km2 "Area of grid cell"
sort objectid
save "proc_data\ETHNICgreg.dta", replace

keep objectid gid_0 g1id bhbrd ethg1_*
foreach X in ctr nga bhbr {
gen `X'_sh = ethg1_`X'_km2/ethg1_km2*100
sum `X'_sh, d
}

*keep if gid_0 != "NGA"
*tab gid_0 
*sort objectid g1id
*order objectid g1id
*codebook ethg1*
*sum ethg1*

*sort g1id
*br if ethg1_bhbr_km2>0

**
**
**
import dbase using "proc_data\murd_intersect_join_pt.dbf", clear

rename *, lower

keep objectid gid_0 fid_murd_c murd_km2 name-codetype

* dummy coastline misalignment, used spatial join closest to overcome
rename fid_murd_c d_murdcoast
replace d_murdcoast=1 if d_murdcoast>=0
replace d_murdcoast=0 if d_murdcoast==-1
sum d_murdcoast, det

sort objectid
by objectid: egen grid_km2 = total(murd_km2)

merge m:1 objectid using "proc_data\TREATbhbr.dta"

drop _merge

** tribe level anywhere
sort tribe_code
by tribe_code: egen ethm_km2 = total(murd_km2)

* total area of tribe in Nigeria
sort tribe_code
g ethm_nga_km2_only = murd_km2 if gid_0=="NGA"
sort tribe_code 

*br if ethm_nga_km2_only!=. & gid_0!="NGA"
sort tribe_code
by tribe_code: egen ethm_nga_km2 = total(ethm_nga_km2_only)

* total area of ethnic group in cell's country
sort tribe_code gid_0
by tribe_code gid_0: egen ethm_ctr_km2 = total(murd_km2)

* total area of tribe in BHbr
sort tribe_code
g ethm_bhbr_km2_only = murd_km2 if bhbrd==1
sort tribe_code
by tribe_code: egen ethm_bhbr_km2 = total(ethm_bhbr_km2_only)

drop ethm_nga_km2_only
drop ethm_bhbr_km2_only

** culturegrp level

*drop fid_gsm200 iso_a2code km2_gsm09 gridkm2
label variable ethm_km2 "Total area of ethnic group : Murdock (2011)"
label variable ethm_nga_km2 "Area of ethnic group in NGA : Murdock (2011)"
label variable ethm_ctr_km2 "Area of ethnic group in cell's country : Murdock (2011)"
label variable ethm_bhbr_km2 "Area of ethnic group in BHbr : Murdock (2011)"

label variable murd_km2 "Area of ethnic group shape x grid : Murdock (2011)"
label variable grid_km2 "Area of grid cell"


save "proc_data\ETHNICmurd.dta", replace


keep objectid gid_0 bhbrd tribe_code ethm_*
foreach X in ctr nga bhbr {
gen `X'_sh = ethm_`X'_km2/ethm_km2*100
sum `X'_sh, d
}


*keep if gid_0 != "NGA"
*tab gid_0 
*sort objectid tribe_code
*order objectid tribe_code
*codebook ethm*
*sum ethm*

*sort tribe_code
*br if ethm_bhbr_km2>0

